Skip to content

Conversation

@Marius-Juston
Copy link
Contributor

The minmax function implementation

This pull request implements the minmax function, which is very similar to the min and max functions; however, makes it much more efficient to compute both the min and the max values in a single pass. This is extremely useful for a lot of applications, for example it can be used inside the textwrap.dedent (#131919 ) function inside Python, the min and max functions are also very often used inside statical methods to compute the range of values.

The advantage of this system is that instead of doing 2 passes inside the dataset / iterable, it can be once.

The function would be defined as:

(min_val, max_val) = minmax([1,2,34,45,6])

This probably needs a PEP (I am just familiar enough with the process so I am putting Pull request first)

@Marius-Juston
Copy link
Contributor Author

Also inside the min_max function that is already defined it would probably make it more readable if:

(line 2044 of bltinmodule.c)

if (cmp < 0)
    goto Fail_it_item_and_val;

and

(line 2067 of bltinmodule.c)

else
    Py_DECREF(maxval);

used brackets as,

(line 2044 of bltinmodule.c)

if (cmp < 0) {
    goto Fail_it_item_and_val;
}

and

(line 2067 of bltinmodule.c)

else {
    Py_DECREF(maxval);
}

I can add this to my pull request if we want as well.

@Marius-Juston
Copy link
Contributor Author

It might not necessary make complete sense for this function to be part of the builtin since it's functionality is so close to min and max and maybe it should be moved to the math portion of the library instead or something?

@picnixz picnixz closed this Feb 1, 2026
@picnixz
Copy link
Member

picnixz commented Feb 1, 2026

Please do not create PRs for features without a discussion. This is not the way contribution works here. The workflow is explained in the devguide which was referenced in this repo's README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants